// 包名
package com.example.chatuidemo.slice;
// 模块引入
import com.example.chatuidemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 进行TabList和PageSlider的初始化
        setupTabList();
        setupPageSlider();
    }
    // 定义私有属性
    private TabList tabList;
    private PageSlider pageSlider;
    // 进行TabList的初始化
    private void setupTabList() {
        // 获取组件
        tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        // 定义两个标签
        TabList.Tab tab1 = tabList.new Tab(getContext());
        tab1.setText("消息");
        tabList.addTab(tab1);
        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("联系人");
        tabList.addTab(tab2);
        // 默认选中第一个标签
        tabList.selectTabAt(0);
        // 添加选中标签变化的监听
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 选中标签时，对应的切换PageSlider当前的页面
                pageSlider.setCurrentPage(tab.getPosition(), true);
            }
            @Override
            public void onUnselected(TabList.Tab tab) {}
            @Override
            public void onReselected(TabList.Tab tab) {}
        });
    }
    // 初始化PageSlider
    private void setupPageSlider() {
        // 获取组件
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        // 设置数据源
        pageSlider.setProvider(new MessagePageProvider(getContext()));
        // 添加页面切换的监听
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) { }
            @Override
            public void onPageSlideStateChanged(int i) { }
            @Override
            public void onPageChosen(int i) {
                // 页面切换后，同步更改选中的标签
                tabList.selectTabAt(i);
            }
        });
    }
}
